<template>
	<view class="container">
		<uni-forms ref="valiForm" :modelValue="baseFormData" label-position="top" :rules="rules">
			<uni-forms-item label="车架号" name="vin" required>
				<uni-easyinput v-model="baseFormData.vin" placeholder="请输入车架号" />
			</uni-forms-item>
			<uni-forms-item label="车型" name="hpzl" required>
				<uni-easyinput v-model="baseFormData.hpzl" placeholder="请输入车型" />
			</uni-forms-item>
			<uni-forms-item label="里程表" name="lCb" required>
				<uni-easyinput v-model="baseFormData.lCb" placeholder="请输入里程表" />
			</uni-forms-item>
			<uni-forms-item label="车牌号" name="hphm" required>
				<uni-easyinput v-model="baseFormData.hphm" placeholder="请输入车牌号" />
			</uni-forms-item>
			<uni-forms-item label="客户类型" name="cailiao" required>
				<uni-data-checkbox v-model="baseFormData.cailiao" :localdata="cailiaoOptions" />
			</uni-forms-item>
			<uni-forms-item label="采购员" name="name" required>
				<uni-easyinput v-model="baseFormData.name" placeholder="请输入采购员" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submit('valiForm')" style="background-color:#409Eff">提交</button>
	</view>
</template>

<script>
	import {
		carRedo,
		addcar
	} from '@/api/app.js'
	export default {
		data() {
			return {
				baseFormData: {
					vin: '',
					hpzl: '',
					lCb: '',
					hphm: '',
					cailiao: '',
					name: '',
					memberId: ''
				},
				cailiaoOptions: [{
						text: '自然',
						value: '自然'
					},
					{
						text: '倒流',
						value: '倒流'
					},
					{
						text: '同行',
						value: '同行'
					},
					{
						text: '老客户',
						value: '老客户'
					}
				],
				// 校验规则
				rules: {
					vin: {
						rules: [{
							required: true,
							errorMessage: '车架号不能为空'
						}]
					},
					hpzl: {
						rules: [{
							required: true,
							errorMessage: '车型不能为空'
						}]
					},
					lCb: {
						rules: [{
							required: true,
							errorMessage: '里程表不能为空'
						}]
					},
					hphm: {
						rules: [{
							required: true,
							errorMessage: '车牌号不能为空'
						}]
					},
					cailiao: {
						rules: [{
							required: true,
							errorMessage: '客户类型不能为空'
						}]
					},
					name: {
						rules: [{
							required: true,
							errorMessage: '采购员不能为空'
						}]
					}
				},
			}
		},
		onLoad() {
			const memberId = uni.getStorageSync('id')
			const name = uni.getStorageSync('name')
			if (memberId) {
				this.baseFormData.memberId = memberId
				this.baseFormData.name = name
			}
		},
		methods: {
			submit(ref) {
				this.$refs[ref].validate().then(async res => {
					const isRedo = await carRedo({
						vin: this.baseFormData.vin
					})
					if (isRedo.code == 1) {
						const content = isRedo.data.map(item => {
							return `当前添加的车辆已存在，车架号${item.vin}-车牌${item.hphm}-时间${item.createTime}`
						}).join('\n')
						uni.showModal({
							title: '车辆已存在',
							content
						})
					} else {
						this.addcar()
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
			// 添加车辆
			async addcar() {
				const res = await addcar(this.baseFormData)
				if (res.code === 1) {
					uni.showToast({
						icon: "none",
						title: '添加成功'
					})

					for (let key in this.baseFormData) {
						if (key != 'name') {
							this.baseFormData[key] = ''
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background-color: #ffffff;
		box-sizing: border-box;
		padding: 20rpx;
	}
</style>